package com.gxnu.edu.cn.client.controller;

import com.gxnu.edu.cn.ttxxt.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
public class UserClient {
    @Autowired
    private RestTemplate restTemplate;

    private String baseUrl = "http://localhost:8080";

    public CompletableFuture<Map<String, Object>> getUserByUsername(String username) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<Map> response = restTemplate.getForEntity(
                        baseUrl + "/api/users/username/" + username,
                        Map.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("获取用户信息失败: " + e.getMessage(), e);
            }
        });
    }


    public CompletableFuture<Map<String, Object>> login(Map<String, String> credentials, String ip) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Map<String, String> requestBody = new HashMap<>();
                requestBody.put("username", credentials.get("username"));
                requestBody.put("password", credentials.get("password"));
                requestBody.put("ip", ip); // 添加 IP 地址
                ResponseEntity<Map> response = restTemplate.postForEntity(
                        baseUrl + "/api/users/login",
                        requestBody,
                        Map.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("登录失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<Map<String, Object>> register(Users users) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<Map> response = restTemplate.postForEntity(
                        baseUrl + "/api/users/register",
                        users,
                        Map.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("注册失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<Map<String, Object>> getUserInfo(Long userId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<Map> response = restTemplate.getForEntity(
                        baseUrl + "/api/users/" + userId,
                        Map.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("获取用户信息失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<Map<String, Object>> updateUserInfo(Long userId, Users updatedUsers) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<Map> response = restTemplate.exchange(
                        baseUrl + "/api/users/" + userId,
                        org.springframework.http.HttpMethod.PUT,
                        new org.springframework.http.HttpEntity<>(updatedUsers),
                        Map.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("更新用户信息失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<Map<String, Object>> changePassword(Long userId, Map<String, String> request) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<Map> response = restTemplate.exchange(
                        baseUrl + "/api/users/" + userId + "/password",
                        org.springframework.http.HttpMethod.PUT,
                        new org.springframework.http.HttpEntity<>(request),
                        Map.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("修改密码失败: " + e.getMessage(), e);
            }
        });
    }
}